<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑':'新增' " :visible="showDialog" @close="closeDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="checkForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="user in employeeList" :key="user.id" :label="user.username" :value="user.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnAdd">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDeparmentList, creatorDepartment, editDepartments } from '@/api/department'
import { getEmploeeSimpleInfo } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // 用户点击的子部门
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const CheckRepeatName = async(rule, val, callback) => {
      // 如果有id才是编辑，否则是新增
      const { depts } = await getDeparmentList()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.filter(item => item.pid === this.nodeData.pid).some(item => item.name === val && item.id !== this.nodeData.id)
      } else {
        isRepeat = depts.filter(item => item.pid === this.nodeData.id).some(item => item.name === val)
      }
      if (isRepeat) {
        callback(new Error('部门名称不能重复'))
      } else {
        callback()
      }
    }
    const CheckRepeatCode = async(rule, val, callback) => {
      const { depts } = await getDeparmentList()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.filter(item => item.pid === this.nodeData.pid).some(item => item.code === val && item.id !== this.nodeData.id)
      } else {
        isRepeat = depts.some(item => item.code === val)
      }

      isRepeat ? callback(new Error('code全公司唯一，不能重复')) : callback()
    }
    return {
      // 定义表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },

      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { max: 10, message: '部门名称要求1-10个字符', trigger: 'blur' },
          { validator: CheckRepeatName, trigger: 'blur' }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { max: 10, message: '部门编码要求1-10个字符', trigger: 'blur' },
          { validator: CheckRepeatCode, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', max: 300, message: '部门介绍要求1-50个字符' }]
      },
      // 收集部门负责人信息
      employeeList: []
    }
  },
  async created() {
    this.employeeList = await getEmploeeSimpleInfo()
  },
  methods: {
    // 点击增加部门
    async btnAdd() {
      // 校验表达
      await this.$refs.checkForm.validate()
      // 请求数据
      await creatorDepartment({
        ...this.formData,
        pid: this.nodeData.id
      })
      // 通知父组件刷新页面
      this.$emit('addDepartmens')
      // 关闭dialog
      this.$emit('update:showDialog', false)
      // 提示
      this.$message.success('新增成功')
    },
    // 点击取消关闭dialog窗口
    btnCancel() {
      // 清空form表达数据
      this.$refs.checkForm.resetFields()

      // 关闭dialog
      this.$emit('update:showDialog', false)
    },
    // 关闭dialog窗口
    closeDialog() {
      // 清空form表达数据
      this.$refs.checkForm.resetFields()
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 关闭dialog
      this.$emit('update:showDialog', false)
    },
    async editDepartments(id) {
      this.formData = await editDepartments(id)
    }

  }
}
</script>
<style>

</style>
